package com.ghy.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ghy.pojo.User;
import com.ghy.service.UserService;
import com.ghy.mapper.UserMapper;
import com.ghy.utils.JwtHelper;
import com.ghy.utils.MD5Util;
import com.ghy.utils.Result;
import com.ghy.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
* @author 龚毫杨
* @description 针对表【news_user】的数据库操作Service实现
* @createDate 2024-10-15 17:18:12
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{
    @Autowired
    private JwtHelper jwtHelper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Result login(User user) {
        LambdaQueryWrapper<User> querWrapper = new LambdaQueryWrapper<>();
        querWrapper.eq(User::getUsername, user.getUsername());
        User loginUser = userMapper.selectOne(querWrapper);

        //账号判断
        if(loginUser == null){
            return Result.build(null, ResultCodeEnum.USERNAME_ERROR);
        }
        //密码判断
        if(!StringUtils.isEmpty(user.getUserPwd()) && loginUser.getUserPwd().equals(MD5Util.encrypt(user.getUserPwd()))){
            //账号密码正确，生成token
            String token = jwtHelper.createToken(Long.valueOf(loginUser.getUid()));

            HashMap data = new HashMap();
            data.put("token", token);
            return Result.ok(data);
        }
        return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
    }

    //根据token获取数据
    @Override
    public Result getUserInfo(String token) {
        //1.判断令牌是否过期 过期true 不过期false
        if(jwtHelper.isExpiration(token)){
            Result.build(null,ResultCodeEnum.NOTLOGIN);
        }
        //获取token对于的用户
        int userId = jwtHelper.getUserId(token).intValue();

        //查询数据
        User user = userMapper.selectById(userId);
        if(user != null){
            user.setUserPwd(null);
            HashMap data = new HashMap();
            data.put("loginUser", user);
            return Result.ok(data);
        }
        return Result.build(null,ResultCodeEnum.NOTLOGIN);
    }

    @Override
    public Result checkUserName(String username) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        User user = userMapper.selectOne(queryWrapper);
        if(user != null){
            return Result.build(null, ResultCodeEnum.USERNAME_USED);
        }
        return Result.ok(user);

    }

    @Override
    public Result regist(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, user.getUsername());
        Long count = userMapper.selectCount(queryWrapper);
        if(count > 0){
            return Result.build(null, ResultCodeEnum.USERNAME_USED);
        }
        user.setUserPwd(MD5Util.encrypt(user.getUserPwd()));
        int row = userMapper.insert(user);
        System.out.println("row =" + row );
        return  Result.ok(user);
    }

    @Override
    public Result checkLogin(String token) {
        //没有传或过期
        if (StringUtils.isEmpty(token) && jwtHelper.isExpiration(token)){
            return Result.build(null,ResultCodeEnum.NOTLOGIN);
        }
        return Result.ok(null);

    }
}




